/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 angle.cpp

	$Header: /heroes4/angle.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "angle.h"

#include <assert.h>
#include <math.h>
#include "map_point.h"
#include "math_constants.h"

// calculate angles in degrees
int get_angle( t_map_point_2d const& vector )
{
	int result = atan2( (float)vector.column, -(float)vector.row ) * 180.0 / k_pi;

	return (result + 360) % 360;
}

// angle difference, 0-180 degrees
int get_difference( int left_angle, int right_angle )
{
	int result = abs(left_angle - right_angle) % 360;

	if (result > 180)
		result = 360 - result;
	return result;
}

// convert an angle in degrees to a direction
t_direction get_direction( int angle )
{
	assert( angle >= 0 );
	angle <<= 1;
	angle = (angle + 45) / 90;
	return t_direction(angle & 7);
}

// -----------------------------------------------------------------------
// determine direction from one actor to another
// -----------------------------------------------------------------------
t_direction get_direction( t_map_point_2d const& left_position,
						   t_map_point_2d const& right_position )
{
	int angle = get_angle( right_position - left_position );
	return get_direction( angle );
}

int  get_angle( t_map_point_2d const& left, t_map_point_2d const& right )
{
	return get_angle( right - left );
}


int  get_map_angle( t_screen_point const& left, t_screen_point const& right )
{
	t_screen_point delta = right - left;
	int			   angle;

	angle = atan2( (float)delta.y / 2, (float)delta.x ) * 180.0 / k_pi;
	angle += 45;
	return (angle + 360) % 360;
}
